const webpack = require('webpack')

const path = require('path')
const defaultSettings = require('./src/settings.js')

function resolve(dir) {
  return path.join(__dirname, dir)
}

const name = defaultSettings.title // page title

// If your port is set to 80,
// use administrator privileges to execute the command line.
// For example, Mac: sudo npm run
// You can change the port by the following methods:
// port = 9528 npm run dev OR npm run dev --port = 5555
const port = process.env.port || process.env.npm_config_port || 80 // dev port

let proxyApi = process.env.VUE_APP_BASE_API

let printApi = process.env.VUE_APP_PRINT_API

// All configuration item explanations can be find in https://cli.vuejs.org/config/
module.exports = {
  /**
   * You will need to set publicPath if you plan to deploy your site under a sub path,
   * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,
   * then publicPath should be set to "/bar/".
   * In most cases please use '/' !!!
   * Detail: https://cli.vuejs.org/config/#publicpath
   */
  transpileDependencies: ['@jiaminghi/data-view'],
  publicPath: defaultSettings.pathRoot,
  outputDir: 'dist',
  assetsDir: 'static',
  lintOnSave: process.env.NODE_ENV === 'development',
  // lintOnSave: false,
  productionSourceMap: false,
  devServer: {
    port,
    open: false,
    overlay: {
      warnings: true,
      errors: true
    },
    proxy: {
      '/api': {
        target: proxyApi, // 对应自己的接口
        changeOrigin: true,
        ws: true,
        pathRewrite: {
          '^/api': ''
        }
      },
      '/ws': {
        target: proxyApi, // 对应自己的接口
        changeOrigin: true,
        ws: true,
        pathRewrite: {
          '^/ws': ''
        }
      },
      '/res': {
        target: proxyApi, // 对应自己的接口
        changeOrigin: true,
        ws: true
      },
      '/upload': {
        target: proxyApi, // 对应自己的接口
        changeOrigin: true,
        ws: true
      },
      '/print': {
        target: printApi,
        changeOrigin: true,
        ws: true,
        pathRewrite: {
          '^/print': ''
        }
      },
      '/logs': {
        target: proxyApi,
        changeOrigin: true,
        ws: true,
        pathRewrite: {
          '^/logs': ''
        }
      },
      '/druid': {
        target: proxyApi + 'druid/', // 对应自己的接口
        changeOrigin: true,
        ws: true,
        pathRewrite: {
          '^/druid': ''
        }
      }
    }
  },
  pages: {
    index: {
      // entry for the page
      entry: 'src/main.js',
      // the source template
      template: 'public/index.html',
      // output as dist/index.html
      filename: 'index.html',
      // when using title option,
      // template title tag needs to be <title><%= htmlWebpackPlugin.options.title %></title>
      title: name,
      // chunks to include on this page, by default includes
      // extracted common chunks and vendor chunks.
      chunks: ['chunk-vendors', 'chunk-common', 'index']
    }
  },
  configureWebpack: {
    // provide the app's title in webpack's name field, so that
    // it can be accessed in index.html to inject the correct title.
    name,
    resolve: {
      alias: {
        '@': resolve('src')
      }
    },
    plugins: [
      new webpack.ProvidePlugin({
        $: 'jquery',
        jQuery: 'jquery',
        'windows.jQuery': 'jquery'
      }),
    ]
  }
}
